package com.zuowei.ycy.user.ui.mine;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.and.yzy.frame.util.DensityUtils;
import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.view.dialog.MaterialDialog;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.AddressAdapter;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.AddressInfo;
import com.zuowei.ycy.user.event.AddressEvent;
import com.zuowei.ycy.user.http.Address;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/27.
 * 地址列表
 */
public class AddressListAty extends BaseToolbarAty implements SwipeMenuListView.OnMenuItemClickListener {
    @Bind(R.id.listview)
    SwipeMenuListView listview;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.fbtn_add_address)
    TextView fbtnAddAddress;

    private List<AddressInfo> list;
    private AddressAdapter adapter;
    private AddressInfo info;

    @Override
    public int getLayoutId() {
        return R.layout.address_list_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("收货地址");

        initListMenu();

        tvEmpty.setText("暂无收货地址");
        list = new ArrayList<>();
        adapter = new AddressAdapter(this, list, R.layout.address_item);
        listview.setAdapter(adapter);
        listview.setEmptyView(llEmpty);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (getIntent().getExtras() != null) {
                    AddressInfo info = (AddressInfo) parent.getItemAtPosition(position);

                    Intent intent = getIntent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("info", info);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();

                }

            }
        });

    }

    private void initListMenu() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem item = new SwipeMenuItem(AddressListAty.this);
                item.setBackground(new ColorDrawable(getResources().getColor(R.color.toolbar_bg)));
                item.setWidth(DensityUtils.dp2px(AddressListAty.this, 80));
                item.setIcon(R.drawable.bianji);
                item.setTitle("编辑");
                menu.addMenuItem(item);
                SwipeMenuItem item2 = new SwipeMenuItem(AddressListAty.this);
                item2.setBackground(new ColorDrawable(getResources().getColor(R.color.text_gray)));
                item2.setWidth(DensityUtils.dp2px(AddressListAty.this, 80));
                item2.setIcon(R.drawable.fabu_delete);
                item2.setTitle("删除");
                menu.addMenuItem(item2);
            }
        };

        listview.setMenuCreator(creator);
        listview.setOnMenuItemClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        doHttp(RetrofitUtils.createApi(Address.class).addressList(UserManger.getM_id()), 0);
    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
    }

    @OnClick({R.id.fbtn_add_address})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.fbtn_add_address:
                startActivity(AddaddressAty.class, null);
                break;
        }
    }

    @Override
    public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
        if (index == 1) {
            MaterialDialog dialog = new MaterialDialog(this);
            dialog.setMDTitle("提示");
            dialog.setMDMessage("确定要删除该地址");
            dialog.setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                @Override
                public void dialogBtnOnClick() {
                    info = adapter.getItem(position);
                    showLoadingDialog(null);
                    doHttp(RetrofitUtils.createApi(Address.class).deleteAddress(info.getAddress_id()), 1);
                }
            });
            dialog.show();

        } else {
            Bundle bundle = new Bundle();
            AddressInfo info = adapter.getItem(position);
            bundle.putSerializable("info", info);
            bundle.putString("type", "1");
            startActivity(AddaddressAty.class, bundle);
        }


        return false;
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

        if (what == 0) { //列表
            list.clear();
            list.addAll(AppJsonUtil.getArrayList(result, AddressInfo.class));
            adapter.notifyDataSetChanged();
        } else if (what == 1) { //删除
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            adapter.removeInfo(info);

            if (getIntent().getExtras() != null) {

                EventBus.getDefault().post(new AddressEvent(info.getAddress_id()));

            }

        }
        super.onSuccess(result, call, response, what);
    }
}
